package swing;

import javax.swing.*;
import javax.swing.border.Border;

import java.awt.*;
import java.awt.event.ItemEvent;

public class ComboBoxSelectDemo {
    private static DefaultComboBoxModel<String> model1() {
        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
        model.addElement("A");
        model.addElement("B");
        model.addElement("C");
        return model;
    }
    
    private static DefaultComboBoxModel<String> model2() {
        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
        model.addElement("α");
        model.addElement("β");
        model.addElement("γ");
        return model;
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame form = new JFrame("ComboBox Select");
            form.setVisible(true);
            form.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            form.setSize(600, 400);
            form.setLocationRelativeTo(null);
            Container contentPane = form.getContentPane();
            contentPane.setLayout(new GridLayout(4, 1));
            Border border = BorderFactory.createLineBorder(Color.black);
            JPanel panel = new JPanel();
            panel.setBorder(border);
            Container container = (Container) contentPane.add(panel);
            panel = new JPanel(new GridLayout(3, 1));
            panel.setBorder(border);
            // component的布局是三行一列的网格布局
            JComponent component = (JComponent) contentPane.add(panel, 1);
            panel = new JPanel();
            contentPane.add(panel, 2);
            panel = new JPanel();
            contentPane.add(panel);
            JComboBox<String> comboBox1 = new JComboBox<>();
            JComboBox<String> comboBox2 = new JComboBox<>(model1());
            container.add(comboBox1);
            container.add(comboBox2);
            comboBox1.setEditable(true);
            comboBox1.addItem("第一项");
            comboBox1.addItem("第二项");
            comboBox1.addItem("第三项");
            comboBox1.addItem("第四项");
            comboBox1.addItemListener(e -> {
                // e.getSource() != comboBox.getItemAt(2) && e.getStateChange() == ItemEvent.SELECTED
                if (comboBox1.getSelectedIndex() != 2 && e.getStateChange() == ItemEvent.SELECTED) {
                    JOptionPane.showMessageDialog(null, "你没有选择第三项");
                    comboBox2.setModel(model1());
                } else if (comboBox1.getSelectedIndex() == 2 && e.getStateChange() == ItemEvent.SELECTED) {
                    JOptionPane.showMessageDialog(null, "你选择了第三项");
                    comboBox2.setModel(model2());
                }
            });
            JRadioButton button1 = new JRadioButton("单选按钮1");
            JRadioButton button2 = new JRadioButton("单选按钮2", true);
            JRadioButton button3 = new JRadioButton("单选按钮3");
            FlowLayout layout = new FlowLayout();
            /*
             * component的所有子组件都是面板且布局为流布局
             * panel是component的第一个子组件
             */
            panel = (JPanel) component.add(new JPanel(layout));
            ((JComponent) component.add(new JPanel(layout))).add(button2);
            container = (Container) component.add(new JPanel(layout));
            panel.add(button1);
            container.add(button3);
            /*
             * 创建一个按钮组，这个组内的所有单选按钮相互排斥
             * 如果选择其中一个单选按钮，其他单选按钮将不再被置为已选择的状态
             */
            ButtonGroup group = new ButtonGroup();
            group.add(button1);
            group.add(button2);
            group.add(button3);
        });
    }
}
